from django.shortcuts import render, render_to_response, redirect
from django.http import HttpResponse
from lib.voclib import *
from django.views.decorators.csrf import csrf_exempt


voc = Voc()


def list_words(request, *args):
    context = dict()
    word_list = list()
    context['voc_info'] = voc.voc_info()
    for i in range(0, voc.all_num):
        word_list.append(voc.word_info(i))
    context['word_info'] = word_list
    return render(request, 'vocnote/list.html', context=context)


@csrf_exempt
def add(request, *args):
    info = ''
    eng = ''
    chi = ''
    if request.method == 'POST':
        eng = request.POST['eng']
        chi = request.POST['chi']
        if eng == '':
            info = "English Input Can't Be None."
        elif chi == '':
            info = "Chinese Input Can't Be None."
        elif voc.search(eng) != -1:
            info = "The Words Has Already Existed: " + voc.word_info(voc.search(eng))
        else:
            voc.append(eng, chi)
            voc.save()
            eng = ''
            chi = ''
            info = "Added."
    return render(request, 'vocnote/add.html', context={'info': info,
                                                        'eng': eng,
                                                        'chi': chi
                                                        })


@csrf_exempt
def delete(request, *args):
    eng = ''
    info = ''
    if request.method == 'POST':
        eng = request.POST['eng']
        if eng == '':
            info = "English Input Can't Be None."
        elif voc.search(eng) == -1:
            info = "There's No Word '{0}'".format(eng)
        else:
            voc.delete(voc.search(eng))
            voc.save()
            info = "Word '{0}' Deleted.".format(eng)
            eng = ''
    return render(request, 'vocnote/delete.html', context={'info': info,
                                                           'eng': eng
                                                           })


@csrf_exempt
def review(request, *args):
    info = ''
    if request.method != 'POST':
        voc.review_times += 1
        voc.permute()
        pri_list = list()
        for i in range(0, voc.all_num):
            pri_list.append(cal_pri(voc.vocabulary[i], voc.review_times))
        voc.quick_sort(pri_list, 0, voc.all_num - 1)

        voc.review_times += 1
    elif request.method == 'POST':
        voc.vocabulary[voc.top][2] += 1
        if 'unf' in request.POST:
            voc.vocabulary[voc.top][3] += 1
            info = "I'll See You Later."
        else:
            info = "Well Done."
    voc.top += 1
    voc.save()
    if voc.top > 19:
        voc.top = -1
        voc.done = True
        return redirect('/vocnote/')
    else:
        eng = voc.vocabulary[voc.top][0]
        chi = voc.vocabulary[voc.top][1]
        csv_chi = voc.query_csv(voc.vocabulary[voc.top][0])
    return render(request, 'vocnote/review.html', context={'info': info,
                                                           'eng': eng,
                                                           'chi': chi,
                                                           'csv_chi': csv_chi.split('\n')})


def index(request, *args):
    if voc.done:
        voc.done = False
        return render(request, 'vocnote/index.html', context={'info': 'Review Completed.'})
    else:
        return render_to_response('vocnote/index.html')
